package com.jason.apps.fromdashboard;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.R;
import com.forimageandlist.Adapter;
public class AllProperty extends SherlockFragment{
    OnClickSelectedListener mCallback;

	MyAdapter adapter;
	ConsumeWebService c;
	
	public final static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
	
	Context appContext;
	HashMap<String, String> map;
	 SherlockFragmentActivity fa;
		
	public static final String KEY_ID = "property_id";
	public static final String KEY_NAME = "property_name";
	public static final String KEY_DESCRIPTION = "property_description";
	public static final String KEY_PRICE = "price";
	public static final String KEY_LATITUDE = "latitude";
	public static final String KEY_LONGITUDE = "longitude";
	public static final String KEY_ADDRESS = "address";
	public static final String KEY_TYPE = "type";
	public static final String KEY_IMAGE = "main_image";
	public static final String KEY_PERSONFNAME="firstname";
	public static final String KEY_PERSONLNAME="lastname";
	public static final String KEY_PERSONCONTACT="mobile";
	
	private ListView list;
	public static int property_id=-1;
		
	public void displayList(){
		c = new ConsumeWebService();
		 String data = c.getAllProperty();
		 
		 try {

				JSONArray array = new JSONArray(data);
				
				for (int i = 0; i < array.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject e = array.getJSONObject(i);
					map.put("property_id", e.getString(KEY_ID));
					map.put("property_name", e.getString(KEY_NAME));
					map.put("property_description", e.getString(KEY_DESCRIPTION));
					map.put("price", e.getString(KEY_PRICE));
					map.put("latitude", e.getString(KEY_LATITUDE));
					map.put("longitude", e.getString(KEY_LONGITUDE));
					map.put("address", e.getString(KEY_ADDRESS));
					map.put("type", e.getString(KEY_TYPE));
					map.put("image", e.getString(KEY_IMAGE));
					map.put("firstname", e.getString("firstname"));
					map.put("lastname", e.getString("lastname"));
					map.put("mobile", e.getString("mobile"));
					map.put("email", e.getString("email_address"));
					
					mylist.add(map);
				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString()+"diri jud");
			}finally{
				
			}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.listforeditproperty, container, false);
		
		fa = (SherlockFragmentActivity) super.getActivity();		
		mylist.clear();
		new Load().execute();
		adapter=new MyAdapter(getActivity(),mylist);
		list=(ListView)view.findViewById(R.id.list_editproperty);
		
		
			     
		return view;
    }
	 @Override
	    public void onAttach(Activity activity) {
	        super.onAttach(activity);
	        
	        try {
	            mCallback = (OnClickSelectedListener) activity;
	        } catch (ClassCastException e) {
	            throw new ClassCastException(activity.toString()
	                    + " must implement OnHeadlineSelectedListener");
	        }
	    }

	    public interface OnClickSelectedListener {
	        /** Called by HeadlinesFragment when a list item is selected */
	        public void onArticleSelected(View v,int position);
	    }
	    
	    public class Load extends AsyncTask<Void, Void, Void> {

	     private final ProgressDialog dialog = new ProgressDialog(fa);

	  	    protected void onPreExecute() {

	  	            this.dialog.setMessage("Loading...");
	  	            this.dialog.show();
	  	    }
		    protected Void doInBackground(final Void... unused) {
		    	
				try {
					displayList();	
			
				} catch (Exception e) {
					Log.e("","wala jud ma add",e);
				}
		   
		        return null; 
		    }
		    protected void onPostExecute(Void result) {
		    	
				list.setAdapter(adapter);
				
				if (this.dialog!=null) {
		            this.dialog.dismiss();
		            }  
		     }
		  }	    
    
}